-module(color).
-export([avg/2, new/3, split/1, clip/1, white/0, black/0, distance/2, scale/2]).
-export([add/2, mul/2]).

-record(color, {r=0, g=0, b=0}).

mul(#color{r=R1, g=G1, b=B1}, #color{r=R2, g=G2, b=B2}) ->
    #color{r=R1*R2, g=G1*G2, b=B1*B2}.

add(#color{r=R1, g=G1, b=B1}, #color{r=R2, g=G2, b=B2}) ->
    #color{r=R1+R2, g=G1+G2, b=B1+B2}.

scale(S, #color{r=R, g=G, b=B}) -> #color{r=R*S, g=G*S, b=B*S}.

distance(#color{r=R1, g=G1, b=B1}, #color{r=R2, g=G2, b=B2}) ->
    vector:distance(vector:new(R1, G1, B1), vector:new(R2, G2, B2)).

avg(#color{r=R1, g=G1, b=B1}, #color{r=R2, g=G2, b=B2}) ->
    #color{r=(R1+R2)*0.5, g=(G1+G2)*0.5, b=(B1+B2)*0.5}.

new(R, G, B) -> #color{r=R, g=G, b=B}.

split(#color{r=R, g=G, b=B}) -> {R, G, B}.

clip(#color{r=R, g=G, b=B}) ->
    #color{
        r=erlang:min(1, erlang:max(0, R)),
        g=erlang:min(1, erlang:max(0, G)),
        b=erlang:min(1, erlang:max(0, B))
    }.

white() -> #color{r=1, g=1, b=1}.

black() -> #color{r=0, g=0, b=0}.